/**
 * 
 */
package com.java.test.messagecenter.mapper;

import com.java.test.messagecenter.dto.PushMessageInfo;
import com.java.test.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Administrator
 *
 */
@Component
public class PushMessageMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int selectpushmessageCount(PushMessageInfo info) {
		String sql = "select count(*) from message_pushmessage where 1=1 ";

		if (info.getPushType() != null && !"".equals(info.getPushType())) {
			sql += "	and push_type ='" + info.getPushType() + "'";
		}

		if (info.getPushTime() != null) {
			sql += " and TO_DAYS(push_time)= TO_DAYS('" + info.getPushTime() + "')";
		}

		if (info.getTitle() != null && !"".equals(info.getTitle())) {

			sql += " and titile like '%" + info.getTitle() + "%'";
		}

		Integer i = jdbcTemplate.queryForObject(sql, Integer.class);

		return i == null ? null : i;

	}

	public List<PushMessageInfo> selectpushmessageList(PushMessageInfo info, PageInfo<PushMessageInfo> page) {

		String sql = "select * from message_pushmessage where 1=1 ";

		if (info.getPushType() != null && !"".equals(info.getPushType())) {
			sql += "	and push_type ='" + info.getPushType() + "'";
		}

		if (info.getPushTime() != null) {
			sql += " and TO_DAYS(push_time)= TO_DAYS('" + info.getPushTime() + "')";
		}

		if (info.getTitle() != null && !"".equals(info.getTitle())) {

			sql += " and titile like '%" + info.getTitle() + "%'";
		}
		int start = (page.getCurrentPage() - 1) * page.getPageCount();
		int limit = page.getPageCount();

		sql += " order by push_time desc limit ?,?";

		List<PushMessageInfo> list = jdbcTemplate.query(sql, new PushMessageInfo(), start, limit);

		return list;

	}

	public List<PushMessageInfo> selectpushmessageTimer(LocalDateTime time) {
		String sql = "select * from message_pushmessage where TO_DAYS(push_time)= TO_DAYS('" + time + "')";

		List<PushMessageInfo> list = jdbcTemplate.query(sql, new PushMessageInfo());

		return list;

	}

	public PushMessageInfo selectpushmessagebyId(PushMessageInfo info) {

		String sql = "select * from message_pushmessage where id=?";

		PushMessageInfo infos = jdbcTemplate.queryForObject(sql, new PushMessageInfo(), info.getId());

		return infos;

	}

	public int insertpushmessageInfo(PushMessageInfo info) {

		String sql = " insert into message_pushmessage(push_type,titile,content,pushitem_type,pushitem_content,push_time,pushitem_account)values"
				+ "(?,?,?,?,?,?,?)";

		int i = jdbcTemplate.update(sql, info.getPushType(), info.getTitle(), info.getContent(), info.getPushitemType(),
				info.getPushitemContent(), info.getPushTime(), info.getPushItemAccount());

		return i;

	}

	/**
	 * 撤销站内的邮件推送
	 * @param now
	 * @author 
	 * @date 2020/7/31 0031 16:38 
	 * @return java.util.List<com.java.test.messagecenter.dto.PushMessageInfo>
	 */
	public List<PushMessageInfo> selectPushMessageList(LocalDateTime now) {

		String sql = "SELECT * FROM message_pushmessage t WHERE t.STATUS = 0 AND t.push_type = 2 AND t.pushitem_account is NOT NULL AND t.push_time <= ?";

		List<PushMessageInfo> list = jdbcTemplate.query(sql, new PushMessageInfo(),now);

		return list;

	}

	public void updatePushMessageStatus(String id) {

		String sql = "update message_pushmessage set status=1  where id = ?";

		int i = jdbcTemplate.update(sql, id);
	}

}
